package leetCode.secondWeek;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Code_25 {
  public ListNode reverseKGroup(ListNode head, int k) {
    List<Integer> res = new LinkedList<>();
    ListNode headCopy = head;
    Stack<Integer> tmp = new Stack<>();

    while (headCopy != null) {
      tmp.push(headCopy.val);
      headCopy = headCopy.next;
      if (tmp.size() == k) {
        while (tmp.size() != 0) {
          res.add(tmp.pop());
        }
        continue;
      }
    }

    for (Integer i : tmp) {
      res.add(i);
    }

    ListNode result = new ListNode(0);
    ListNode resFinal = result;
    for (int i = 0; i < res.size(); i++) {
      result.next = new ListNode(res.get(i));
      result = result.next;
    }

    System.out.println("OK");

    return resFinal.next;
  }
}
